package edu.hawaii.halealohacli.command;

/**
 * Provides the ability to save generic data and compare a specific attribute. Useful in
 * Collections that need to be sorted on one attribute.
 * 
 * @author Andrea Connell
 * 
 * @param <T> The type of the attribute to sort on.
 */
public class SortingObject<T extends Comparable<? super T>> implements
    Comparable<SortingObject<T>> {

  T key;
  Object value;

  /**
   * Provide two attributes for this SortingObject.
   * 
   * @param key The attribute that is Comparable.
   * @param value An attribute to store with the key.
   */
  public SortingObject(T key, Object value) {
    this.key = key;
    this.value = value;
  }

  /**
   * Compare two SortingObjects based on energy, sourceName, and otherAttr2. If all three
   * attributes are the same, the object is equal.
   * 
   * Suppresses unchecked warnings because I couldn't get it to cast to SortingObject<T> without
   * it.
   * 
   * @param obj The SortingObject to compare to.
   * @return True if both attributes are the equal.
   */
  @Override
  @SuppressWarnings({ "unchecked" })
  public boolean equals(Object obj) {
    if (obj instanceof SortingObject<?>) {
      SortingObject<T> sObj = (SortingObject<T>) obj;
      return this.key.equals(sObj.key) && this.value.equals(sObj.value);
    }
    return false;
  }

  /**
   * Calculate hashCode based on energy and sourceName.
   * 
   * @return The sum of the hashCodes of the two attributes.
   */
  @Override
  public int hashCode() {
      return this.key.hashCode() + this.value.hashCode();
  }

  /**
   * Compares energy of this object with energy of o.
   * 
   * @param o The SortingObject to compare to.
   * @return The result of compareTo on the two energy attributes.
   */
  @Override
  public int compareTo(SortingObject<T> o) {
    return this.key.compareTo(o.key);
  }
}
